package com.yeyks.config;

import com.yeyks.aop.AuthorizationInterceptor;
import com.yeyks.aop.WhiteListInterceptor;
import com.yeyks.resolver.LoginUserHandlerMethodArgumentResolver;
import com.yeyks.web.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * @Author fuchangshuai
 * @date 2019/3/11 11:31 AM
 */
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;
    @Autowired
    private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;
    @Autowired
    private TokenInterceptor loginInterceptor;
    @Autowired
    private WhiteListInterceptor whiteListInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**");
        registry.addInterceptor(whiteListInterceptor);
        InterceptorRegistration interceptor = registry.addInterceptor(loginInterceptor);
        // 配置不拦截swagger
        interceptor.excludePathPatterns("/swagger-resources/**",
                "/webjars/**",
                "/swagger-ui.html/**");
        // 配置拦截的路径
        interceptor.addPathPatterns("/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
    }

    /**
     * 配置swagger文件资源
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
